home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / pseudoDoc / pseudoDoc.pl < prev    next >
Encoding:
Perl Script  |  2001-06-23  |  4.4 KB  |  191 lines

  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use Cwd;
  5. use File::Find;
  6. use File::Copy;
  7. use Text::Bastardize;
  8. use HTML::Parser 3.05;
  9. use Getopt::Std;
  10.  
  11. my $pathSeparator;
  12. my $isMacOS;
  13. my $scriptDir = cwd();
  14. BEGIN {
  15.     if ($^O =~ /MacOS/i) {
  16.             $pathSeparator = ":";
  17.             $isMacOS = 1;
  18.     } else {
  19.             $pathSeparator = "/";
  20.             $isMacOS = 0;
  21.     }
  22. }
  23.  
  24. my %options = ();
  25. my $modifier="rev";
  26. &getopts("b:", \%options);
  27. if ($options{b}) {
  28.     my $bastardizationMode;
  29.     $bastardizationMode = $options{b};
  30.     SWITCH: { # determine which type of comment we're in
  31.         ($bastardizationMode =~ /^rev/i) && do {$modifier = "rev"; last SWITCH;};
  32.         ($bastardizationMode =~ /^red/i) && do {$modifier = "rdct"; last SWITCH;};
  33.         ($bastardizationMode =~ /^pig/i) && do {$modifier = "pigIt"; last SWITCH;};
  34.         ($bastardizationMode =~ /^cen/i) && do {$modifier = "censor"; last SWITCH;};
  35.         print "Unknown bastardization mode--we'll reverse the text anyway.\n";
  36.     }
  37. }
  38.  
  39.  
  40. ########################## Input Folder and Files #######################
  41. my @inputFiles;
  42. my $inputDir;
  43.  
  44. if (($#ARGV == 0) && (-d $ARGV[0])) {
  45.     $inputDir = $ARGV[0];
  46.     $inputDir =~ s|(.*)/$|$1|; # get rid of trailing slash, if any
  47.     if ($inputDir !~ /^\//) { # not absolute path -- !!! should check for ~
  48.         $inputDir = $scriptDir.$pathSeparator.$inputDir;
  49.     }
  50.     &find({wanted => \&getFiles, follow => 1}, $inputDir);
  51. } else {
  52.     die "You must specify a single input directory for processing.\n";
  53. }
  54. unless (@inputFiles) { print "No valid input files specified. \n\n";};
  55.  
  56. sub getFiles {
  57.     my $filePath = $File::Find::name;
  58.     my $fileName = $_;
  59.     
  60.     if ($fileName =~ /\.html$/) {
  61.         push(@inputFiles, $filePath);
  62.     }
  63. }
  64.  
  65.  
  66. my $fileString;
  67. foreach my $file (@inputFiles) {
  68.     $fileString='';
  69.     my $p = HTML::Parser->new(unbroken_text => 0,
  70.                   default_h => [ \&printIt, "text" ],
  71.                   text_h    => [ \&$modifier,"text" ],);
  72.                   
  73.     $p->parse_file($file) || die "Can't open file $file: $!\n";
  74.     
  75.     # my @pathParts = split (/\//, $file);
  76.     # my $fileName = pop (@pathParts);
  77.  
  78.     # my $filePathWithoutFilename = join("/", @pathParts);
  79.     # my $archiveName = $fileName."~"; 
  80.     # my $archive = $filePathWithoutFilename."/".$archiveName; 
  81.     if (unlink($file)) {
  82.         open (FILE, ">$file") || die "Can't open file $file.\n";
  83.         print FILE $fileString;
  84.         close (FILE);
  85.     } else {
  86.         die("Couldn't delete $file.\n");
  87.     }
  88. }
  89.  
  90. sub addToFileString {
  91.     my $addition = shift;
  92.     
  93.     $fileString .= $addition;
  94. }
  95.  
  96.  
  97. sub printIt {
  98.     &addToFileString(@_);
  99. };
  100.  
  101. sub rev {
  102.     my $line = shift;
  103.     if ($line =~ /^\s*$/) {
  104.         &addToFileString("$line");
  105.         return;
  106.     } elsif (length($line)) {
  107.         my $text = new Text::Bastardize;
  108.         my @words = split (/\s+|\n+/, $line);
  109.         foreach my $word (@words) {
  110.             $word =~ s/^\s+|\s+//;
  111.             if ($word =~ /^&/) {
  112.                 &addToFileString($word);
  113.             } else {
  114.                 $text->charge("@{[$word]}");
  115.                 &addToFileString("@{[$text->rev()]} ");
  116.             }
  117.         }
  118.     }
  119.     &addToFileString("\n");
  120. };
  121.  
  122. sub censor {
  123.     my $line = shift;
  124.     if ($line =~ /^\s*$/) {
  125.         &addToFileString("$line");
  126.         return;
  127.     } elsif (length($line)) {
  128.         my $text = new Text::Bastardize;
  129.         my @words = split (/\s+|\n+/, $line);
  130.         foreach my $word (@words) {
  131.             $word =~ s/^\s+|\s+//;
  132.             $text->charge("@{[$word]}");
  133.             &addToFileString("@{[$text->censor()]} ");
  134.         }
  135.     }
  136.     &addToFileString("\n");
  137. };
  138.  
  139. sub rdct {
  140.     my $line = shift;
  141.     if ($line =~ /^\s*$/) {
  142.         &addToFileString("$line");
  143.         return;
  144.     } elsif (length($line)) {
  145.         my $text = new Text::Bastardize;
  146.         my @words = split (/\s+|\n+/, $line);
  147.         foreach my $word (@words) {
  148.             $word =~ s/^\s+|\s+//;
  149.             $text->charge("@{[$word]}");
  150.             &addToFileString("@{[$text->rdct()]} ");
  151.         }
  152.     }
  153.     &addToFileString("\n");
  154. };
  155.  
  156. sub pigIt {
  157.     my $line = shift;
  158.     if ($line =~ /^\s*$/) {
  159.         &addToFileString("$line");
  160.         return;
  161.     } elsif (length($line)) {
  162.         # print "\n----------------\n$line\n=================\n\n";
  163.         my $text = new Text::Bastardize;
  164.         my @words = split (/\s+|\n+/, $line);
  165.         foreach my $word (@words) {
  166.             # print "testing==>'$word'\n";
  167.             if (($word =~ /^([a-z]|[A-Z])+$/i) && ($word =~ /[aeiou]/)) {
  168.                 $word =~ s/^\s+|\s+//;
  169.                 # $word = lc($word);
  170.                 # print "\n---->'$word'\n";
  171.                 $text->charge("@{[$word]}");
  172.                 &addToFileString("@{[$text->pig()]} ");
  173.             } else {
  174.                 &addToFileString("$word ");
  175.             }
  176.         }
  177.     }
  178.     &addToFileString("\n");
  179. };
  180.  
  181.  
  182. sub makeAbsolutePath {
  183.    my $relPath = shift;
  184.    my $relTo = shift;
  185.    if ($relPath !~ /^\//) { # doesn't start with a slash
  186.        $relPath = $relTo."/".$relPath;
  187.    }
  188.    return $relPath;
  189. }
  190.  
  191.